package rpc.netty.coder;

import io.netty.buffer.ByteBuf;
import io.netty.channel.ChannelHandlerContext;
import io.netty.handler.codec.MessageToByteEncoder;
import rpc.common.MyMessageProtocol;

/**
 * 自定义编码器
 */
public class MyMessageToByteEncoder extends MessageToByteEncoder<MyMessageProtocol> {
    /**
     * 编码
     */
    @Override
    protected void encode(ChannelHandlerContext channelHandlerContext, MyMessageProtocol myMessageProtocol, ByteBuf byteBuf) {
        byteBuf.writeInt(myMessageProtocol.getLength());
        byteBuf.writeBytes(myMessageProtocol.getContent());
    }
}
